package com.chen.controller;

import com.chen.dto.AutoAddSubjectDTO;
import com.chen.dto.ExamPaperDTO;
import com.chen.dto.RemoveSubjectFromPaperDTO;
import com.chen.service.CourseService;
import com.chen.service.ExamPaperConnectSubjectService;
import com.chen.service.ExamPaperService;
import com.chen.service.GradeService;
import com.chen.vo.*;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 试卷
 */
@Controller
@RequestMapping("/examPaper")
public class ExamPaperController {

    // 试卷相关
    @Autowired
    private ExamPaperService examPaperService;

    // 年级
    @Autowired
    private GradeService gradeService;

    // 科目
    @Autowired
    private CourseService courseService;

    @Autowired
    private ExamPaperConnectSubjectService examPaperConnectSubjectService;

    /**
     * 分页获取试卷信息
     *
     * @param gradeId
     * @param startPage
     * @param pageShow
     * @return
     */
    @RequestMapping("/examPapers")
    public ModelAndView getCourses(@RequestParam(value = "gradeId", required = false) Integer gradeId,
                                   @RequestParam(value = "startPage", required = false, defaultValue = "1") Integer startPage,
                                   @RequestParam(value = "pageShow", required = false, defaultValue = "10") Integer pageShow) {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/examPapers");

        //计算当前查询起始数据索引
        int startIndex = (startPage - 1) * pageShow;

        List<ExamPaperVO> examPapers = examPaperService.listPage(gradeId, startIndex, pageShow);
        model.addObject("examPapers", examPapers);

        //获取试卷总量
        Long examPaperTotal = examPaperService.findAddCount();
        //计算总页数
        Long pageTotal = 1L;
        if (examPaperTotal % pageShow == 0)
            pageTotal = examPaperTotal / pageShow;
        else
            pageTotal = examPaperTotal / pageShow + 1;
        model.addObject("pageTotal", pageTotal);
        model.addObject("pageNow", startPage);

        return model;
    }

    /**
     * 预添加试卷
     *
     * @return
     */
    @GetMapping("/preAddExamPaper")
    public ModelAndView preAddStudent() {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/examPaperedit");
        List<GradeVO> grades = gradeService.findAllGrade();
        model.addObject("grades", grades);
        return model;
    }

    /**
     * 新增试卷
     *
     * @param dto
     * @return
     */
    @PostMapping("/save")
    public String save(ExamPaperDTO dto) {
        examPaperService.saveExamPaper(dto);
        return "redirect:/examPaper/examPapers";
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @PostMapping("/update")
    public String update(ExamPaperDTO dto) {
        examPaperService.updateExamPaper(dto);
        return "redirect:/examPaper/examPapers";
    }


    /**
     * 进入编辑
     *
     * @param examPaperId
     * @return
     */
    @GetMapping("/edit/{examPaperId}")
    public ModelAndView edit(@PathVariable("examPaperId") Integer examPaperId) {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/examPaperedit");
        List<GradeVO> grades = gradeService.findAllGrade();
        model.addObject("grades", grades);
        ExamPaperVO paper = examPaperService.findById(examPaperId);
        model.addObject("examPaper", paper);
        return model;
    }

    /**
     * 删除试卷
     *
     * @param examPaperId 试卷id
     * @return
     */
    @PostMapping(value = "/del/{examPaperId}")
    public String delExamPaperId(@PathVariable("examPaperId") Integer examPaperId) {
        examPaperService.delExamPaperId(examPaperId);
        return "redirect:/examPaper/examPapers";
    }


    /**
     * 添加试题
     *
     * @param examPaperId
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/autoSubject", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView autoSubject(@RequestParam("examPaperId") Integer examPaperId) throws IOException {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/autoSubjectSelect");
        // 所有年级
        List<GradeVO> grades = gradeService.findAllGrade();
        model.addObject("grades", grades);
        model.addObject("courses", courseService.findAll());
        return model;
    }

    /**
     * 自动对试卷添加试题
     *
     * @param dto
     * @return
     */
    @ResponseBody
    @PostMapping("/autoAddSubject")
    public String isAutoAddSubjectToExamPaper(AutoAddSubjectDTO dto) {
        examPaperService.autoAddSubject(dto);
        return "t";
    }

    /**
     * 查看试题
     *
     * @param examPaperId
     * @param courseName
     * @param courseId
     * @param gradeId
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/getESM", method = {RequestMethod.GET, RequestMethod.POST})
    public String getExamPaperWithSubject(
            @RequestParam(value = "examPaperId", required = false) Integer examPaperId,
            @RequestParam(value = "courseName", required = false) String courseName,
            @RequestParam(value = "courseId", required = false) Integer courseId,
            @RequestParam(value = "gradeId", required = false) Integer gradeId) {
        /*条件处理*/
        List<ESMDTO> esms = examPaperConnectSubjectService.getESM(examPaperId, courseName, courseId, gradeId);
        return new Gson().toJson(esms);
    }

    /**
     * 移除试题
     *
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/removeSubjectFromPaper")
    public String removeSubjectFromPaper(RemoveSubjectFromPaperDTO dto) {
        examPaperConnectSubjectService.removeSubjectFromPaper(dto);
        return "t";
    }


    /**
     * 统计试卷信息
     * @param gradeId 年级编号
     * @param startPage 起始页 默认第一页
     * @param pageShow 页容量 默认10
     * @return
     */
    @RequestMapping("/examPapersData")
    public ModelAndView examPapersData(@RequestParam(value = "gradeId", required = false) Integer gradeId,
                                       @RequestParam(value="startPage", required=false, defaultValue="1") Integer startPage,
                                       @RequestParam(value="pageShow", required=false, defaultValue="100") Integer pageShow, HttpServletRequest request) {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/examPapersData");
        TeacherVO teacherInfo = (TeacherVO)request.getSession().getAttribute("loginTeacher");
        Integer teacherId = teacherInfo.getId();
        List<PapersDataVO> examPapers = examPaperService.getExamPapersData(teacherId);
        model.addObject("examPapersData", examPapers);
        return model;
    }


    /**
     * 清空Session中保存的试题编号集合
     * @return
     */
    @RequestMapping("/clearSubjectIdsWithSession")
    public String isClearChooseSubjectIds() {
        return "redirect:/examPaper/examPapers";
    }


}
